home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility1 / gs261src.zip / GDEVSVGA.C < prev    next >
C/C++ Source or Header  |  1993-05-19  |  22KB  |  738 lines

  1. /* Copyright (C) 1991, 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gdevsvga.c */
  20. /* SuperVGA display drivers for Ghostscript */
  21. #include "memory_.h"
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gxdevice.h"
  25. #include "gdevpcfb.h"
  26. #include "gdevsvga.h"
  27.  
  28. /* The color map for dynamically assignable colors. */
  29. #define first_dc_index 64
  30. private int next_dc_index;
  31. #define dc_hash_size 293    /* prime, >num_dc */
  32. typedef struct { ushort rgb, index; } dc_entry;
  33. private dc_entry dynamic_colors[dc_hash_size + 1];
  34.  
  35. /* Macro for casting gx_device argument */
  36. #define fb_dev ((gx_device_svga *)dev)
  37.  
  38. /* Procedure records */
  39. #define svga_procs(open) {\
  40.     open, gx_default_get_initial_matrix,\
  41.     gx_default_sync_output, gx_default_output_page, svga_close,\
  42.     svga_map_rgb_color, svga_map_color_rgb,\
  43.     svga_fill_rectangle, gx_default_tile_rectangle,\
  44.     svga_copy_mono, svga_copy_color, gx_default_draw_line,\
  45.     svga_get_bits, gx_default_get_props, gx_default_put_props\
  46. }
  47.  
  48. /* Save the controller mode */
  49. private int svga_save_mode = -1;
  50.  
  51. /* ------ Internal routines ------ */
  52.  
  53. #define regen 0xa000
  54.  
  55. /* Construct a pointer for writing a pixel. */
  56. /* Assume 64K pages, 64K granularity. */
  57. /* We know that y is within bounds. */
  58. #define set_pixel_ptr(ptr, fbdev, x, y, wnum)\
  59. {    ulong index = (ulong)(y) * fbdev->raster + (uint)(x);\
  60.     if ( (uint)(index >> 16) != fbdev->page )\
  61.        {    (*fbdev->set_page)(fbdev, (fbdev->page = index >> 16), wnum);\
  62.        }\
  63.     ptr = (fb_ptr)MK_PTR(regen, (ushort)index);\
  64. }
  65. #define set_pixel_write_ptr(ptr, fbdev, x, y)\
  66.   set_pixel_ptr(ptr, fbdev, x, y, fbdev->wnum_write)
  67. #define set_pixel_read_ptr(ptr, fbdev, x, y)\
  68.   set_pixel_ptr(ptr, fbdev, x, y, fbdev->wnum_read)
  69.  
  70. /* Find the graphics mode for a desired width and height. */
  71. /* Set the mode in the device structure and return 0, */
  72. /* or return an error code. */
  73. int
  74. svga_find_mode(gx_device *dev, const mode_info _ds *mip)
  75. {    for ( ; ; mip++ )
  76.     {    if ( mip->width >= fb_dev->width &&
  77.              mip->height >= fb_dev->height ||
  78.              mip[1].mode < 0
  79.            )
  80.         {    fb_dev->mode = mip;
  81.             gx_device_adjust_resolution(dev, mip->width, mip->height, 1);
  82.             fb_dev->raster = fb_dev->width;
  83.             return 0;
  84.         }
  85.     }
  86.     return_error(gs_error_rangecheck);
  87. }
  88.  
  89. /* Set the index for writing into the color DAC. */
  90. #define svga_dac_set_write_index(i) outportb(0x3c8, i)
  91.  
  92. /* Write 6-bit R,G,B values into the color DAC. */
  93. #define svga_dac_write(r, g, b)\
  94.   (outportb(0x3c9, r), outportb(0x3c9, g), outportb(0x3c9, b))
  95.  
  96. /* ------ Common procedures ------ */
  97.  
  98. /* Initialize the device structure and the DACs. */
  99. int
  100. svga_open(gx_device *dev)
  101. {    fb_dev->x_pixels_per_inch =
  102.       fb_dev->y_pixels_per_inch =
  103.         fb_dev->height / PAGE_HEIGHT_INCHES;
  104.     /* Set the display mode. */
  105.     if ( svga_save_mode < 0 )
  106.         svga_save_mode = (*fb_dev->get_mode)();
  107.     (*fb_dev->set_mode)(fb_dev->mode->mode);
  108.     /* Load the color DAC. */
  109.     svga_dac_set_write_index(0);
  110.        {    int c;
  111.         for ( c = 0; c < 64; c++ )
  112.            {    static const byte c2[10] =
  113.                { 0, 42, 0, 0, 0, 0, 0, 0, 21, 63 };
  114.             svga_dac_write(c2[(c >> 2) & 9], c2[(c >> 1) & 9],
  115.                        c2[c & 9]);
  116.            }
  117.        }
  118.     /* Initialize the dynamic color table. */
  119.     memset(dynamic_colors, 0, (dc_hash_size + 1) * sizeof(dc_entry));
  120.     next_dc_index = first_dc_index;
  121.     fb_dev->page = -1;
  122.     return 0;
  123. }
  124.  
  125. /* Close the device; reinitialize the display for text mode. */
  126. int
  127. svga_close(gx_device *dev)
  128. {    if ( svga_save_mode >= 0 )
  129.         (*fb_dev->set_mode)(svga_save_mode);
  130.     svga_save_mode = -1;
  131.     return 0;
  132. }
  133.  
  134. /* Map a r-g-b color to a palette index. */
  135. /* The first 64 entries of the color map are set */
  136. /* for compatibility with the older display modes: */
  137. /* these are indexed as 0.0.R0.G0.B0.R1.G1.B1. */
  138. gx_color_index
  139. svga_map_rgb_color(gx_device *dev, ushort r, ushort g, ushort b)
  140. {
  141. #define cv_bits(v,n) (v >> (gx_color_value_bits - n))
  142.     ushort r5 = cv_bits(r, 5), g5 = cv_bits(g, 5), b5 = cv_bits(b, 5);
  143.     static const byte cube_bits[32] =
  144.        {    0, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  145.         8, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  146.         1, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  147.         9
  148.        };
  149.     uint cx = ((uint)cube_bits[r5] << 2) + ((uint)cube_bits[g5] << 1) +
  150.           (uint)cube_bits[b5];
  151.     ushort rgb;
  152.     register dc_entry _ds *pdc;
  153.     /* Check for a color on the cube. */
  154.     if ( cx < 64 ) return (gx_color_index)cx;
  155.     /* Not on the cube, check the dynamic color table. */
  156.     rgb = (r5 << 10) + (g5 << 5) + b5;
  157.     for ( pdc = &dynamic_colors[rgb % dc_hash_size]; pdc->rgb != 0; pdc++ )
  158.     {    if ( pdc->rgb == rgb )
  159.             return (gx_color_index)(pdc->index);
  160.     }
  161.     if ( pdc == &dynamic_colors[dc_hash_size] )
  162.     {    /* Wraparound */
  163.         for ( pdc = &dynamic_colors[0]; pdc->rgb != 0; pdc++ )
  164.         {    if ( pdc->rgb == rgb )
  165.                 return (gx_color_index)(pdc->index);
  166.         }
  167.     }
  168.     if ( next_dc_index == 256 )
  169.     {    /* No space left, report failure. */
  170.         return gx_no_color_index;
  171.     }
  172.     /* Not on the cube, and not in the dynamic table. */
  173.     /* Put in the dynamic table if space available. */
  174.     {    int i = next_dc_index++;
  175.         pdc->rgb = rgb;
  176.         pdc->index = i;
  177.         svga_dac_set_write_index(i);
  178.         svga_dac_write(cv_bits(r, 6), cv_bits(g, 6), cv_bits(b, 6));
  179.         return (gx_color_index)i;
  180.     }
  181. }
  182.  
  183. /* Map a color code to r-g-b. */
  184. /* This routine must invert the transformation of the one above. */
  185. /* Since this is practically never used, we just read the DAC. */
  186. int
  187. svga_map_color_rgb(gx_device *dev, gx_color_index color, ushort prgb[3])
  188. {    uint cval;
  189.     outportb(0x3c7, (byte)color);
  190. #define dacin() (cval = inportb(0x3c9) >> 1,\
  191.   ((cval << 11) + (cval << 6) + (cval << 1) + (cval >> 4)) >>\
  192.    (16 - gx_color_value_bits))
  193.     prgb[0] = dacin();
  194.     prgb[1] = dacin();
  195.     prgb[2] = dacin();
  196. #undef dacin
  197.     return 0;
  198. }
  199.  
  200. /* Copy a monochrome bitmap.  The colors are given explicitly. */
  201. /* Color = gx_no_color_index means transparent (no effect on the image). */
  202. int
  203. svga_copy_mono(gx_device *dev,
  204.   const byte *base, int sourcex, int sraster, gx_bitmap_id id,
  205.   int x, int y, int w, int h, gx_color_index czero, gx_color_index cone)
  206. {    uint raster = fb_dev->raster;
  207.     ushort limit;
  208.     register int wi;
  209.     uint skip;
  210.     int yi;
  211.     register fb_ptr ptr = (fb_ptr)0;
  212.     const byte *srow;
  213.     uint invert;
  214.     fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
  215.     limit = (ushort)-w;
  216.     skip = raster - w + 1;
  217.     srow = base + (sourcex >> 3);
  218. #define izero (int)czero
  219. #define ione (int)cone
  220.     if ( ione == no_color )
  221.     {    gx_color_index temp;
  222.         if ( izero == no_color ) return 0;    /* no-op */
  223.         temp = czero;
  224.         czero = cone;
  225.         cone = temp;
  226.         invert = ~0;
  227.     }
  228.     else
  229.         invert = 0;
  230.     /* Pre-filling saves us a test in the loop, */
  231.     /* and since tiling is uncommon, we come out ahead. */
  232.     if ( izero != no_color )
  233.         svga_fill_rectangle(dev, x, y, w, h, czero);
  234.     for ( yi = 0; yi < h; yi++ )
  235.        {    const byte *sptr = srow;
  236.         uint bits;
  237.         int bitno = sourcex & 7;
  238.         wi = w;
  239.         if ( PTR_OFF(ptr) <= skip )
  240.         {    set_pixel_write_ptr(ptr, fb_dev, x, y + yi);
  241.         }
  242.         else if ( PTR_OFF(ptr) > limit )
  243.         {    /* We're crossing a page boundary. */
  244.             /* This is extremely rare, so it doesn't matter */
  245.             /* how slow it is. */
  246.             int xi = (ushort)-PTR_OFF(ptr);
  247.             svga_copy_mono(dev, srow, sourcex & 7, sraster,
  248.                 gx_no_bitmap_id, x, y + yi, xi, 1,
  249.                 gx_no_color_index, cone);
  250.             set_pixel_write_ptr(ptr, fb_dev, x + xi, y + yi);
  251.             sptr = srow - (sourcex >> 3) + ((sourcex + xi) >> 3);
  252.             bitno = (sourcex + xi) & 7;
  253.             wi -= xi;
  254.         }
  255.         bits = *sptr ^ invert;
  256.         switch ( bitno )
  257.         {
  258. #define ifbit(msk)\
  259.   if ( bits & msk ) *ptr = (byte)ione;\
  260.   if ( !--wi ) break; ptr++
  261.         case 0:
  262. bit0:            ifbit(0x80);
  263.         case 1:
  264.             ifbit(0x40);
  265.         case 2:
  266.             ifbit(0x20);
  267.         case 3:
  268.             ifbit(0x10);
  269.         case 4:
  270.             ifbit(0x08);
  271.         case 5:
  272.             ifbit(0x04);
  273.         case 6:
  274.             ifbit(0x02);
  275.         case 7:
  276.             ifbit(0x01);
  277. #undef ifbit
  278.             bits = *++sptr ^ invert;
  279.             goto bit0;
  280.         }
  281.         ptr += skip;
  282.         srow += sraster;
  283.        }
  284. #undef izero
  285. #undef ione
  286.     return 0;
  287. }
  288.  
  289. /* Copy a color pixelmap.  This is just like a bitmap, */
  290. /* except that each pixel takes 8 bits instead of 1. */
  291. int
  292. svga_copy_color(gx_device *dev,
  293.   const byte *base, int sourcex, int sraster, gx_bitmap_id id,
  294.   int x, int y, int w, int h)
  295. {    int xi, yi;
  296.     int skip;
  297.     const byte *sptr;
  298.     fb_ptr ptr;
  299.     fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
  300.     skip = sraster - w;
  301.     sptr = base + sourcex;
  302.     for ( yi = y; yi - y < h; yi++ )
  303.        {    ptr = 0;
  304.         for ( xi = x; xi - x < w; xi++ )
  305.            {    if ( PTR_OFF(ptr) == 0 )
  306.                 set_pixel_write_ptr(ptr, fb_dev, xi, yi);
  307.             *ptr++ = *sptr++;
  308.            }
  309.         sptr += skip;
  310.        }
  311.     return 0;
  312. }
  313.  
  314. /* Fill a rectangle. */
  315. int
  316. svga_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  317.   gx_color_index color)
  318. {    uint raster = fb_dev->raster;
  319.     ushort limit = (ushort)-raster;
  320.     int yi;
  321.     fb_ptr ptr;
  322.     fit_fill(dev, x, y, w, h);
  323.     set_pixel_write_ptr(ptr, fb_dev, x, y);
  324.     /* Most fills are very small and don't cross a page boundary. */
  325.     yi = h;
  326.     switch ( w )
  327.        {
  328.     case 0: return 0;        /* no-op */
  329.     case 1:
  330.         while ( --yi >= 0 && PTR_OFF(ptr) < limit )
  331.             ptr[0] = (byte)color,
  332.             ptr += raster;
  333.         if ( !++yi ) return 0;
  334.         break;
  335.     case 2:
  336.         while ( --yi >= 0 && PTR_OFF(ptr) < limit )
  337.             ptr[0] = ptr[1] = (byte)color,
  338.             ptr += raster;
  339.         if ( !++yi ) return 0;
  340.         break;
  341.     case 3:
  342.         while ( --yi >= 0 && PTR_OFF(ptr) < limit )
  343.             ptr[0] = ptr[1] = ptr[2] = (byte)color,
  344.             ptr += raster;
  345.         if ( !++yi ) return 0;
  346.         break;
  347.     case 4:
  348.         while ( --yi >= 0 && PTR_OFF(ptr) < limit )
  349.             ptr[0] = ptr[1] = ptr[2] = ptr[3] = (byte)color,
  350.             ptr += raster;
  351.         if ( !++yi ) return 0;
  352.         break;
  353.     default:
  354.         if ( w < 0 ) return 0;
  355.        }
  356.     while ( --yi >= 0 )
  357.        {    if ( PTR_OFF(ptr) < limit )
  358.            {    memset(ptr, (byte)color, w);
  359.             ptr += raster;
  360.            }
  361.         else if ( PTR_OFF(ptr) <= (ushort)(-w) )
  362.            {    memset(ptr, (byte)color, w);
  363.             if ( yi > 0 )
  364.                 set_pixel_write_ptr(ptr, fb_dev, x, y + h - yi);
  365.            }
  366.         else
  367.            {    uint left = (uint)0x10000 - PTR_OFF(ptr);
  368.             memset(ptr, (byte)color, left);
  369.             set_pixel_write_ptr(ptr, fb_dev, x + left, y + h - 1 - yi);
  370.             memset(ptr, (byte)color, w - left);
  371.             ptr += raster - left;
  372.            }
  373.        }
  374.     return 0;
  375. }
  376.  
  377. /* Read scan lines back from the frame buffer. */
  378. int
  379. svga_get_bits(gx_device *dev, int y, byte *data, byte **actual_data)
  380. {    uint bytes_per_row = dev->width;
  381.     byte *dest = data;
  382.     ushort limit = (ushort)-bytes_per_row;
  383.     fb_ptr src;
  384.     if ( y < 0 || y >= dev->height )
  385.         return gs_error_rangecheck;
  386.     set_pixel_read_ptr(src, fb_dev, 0, y);
  387.     /* The logic here is similar to fill_rectangle. */
  388.     if ( PTR_OFF(src) <= limit )
  389.         memcpy(data, src, bytes_per_row);
  390.     else
  391.        {    uint left = (uint)0x10000 - PTR_OFF(src);
  392.         memcpy(data, src, left);
  393.         set_pixel_read_ptr(src, fb_dev, left, y);
  394.         memcpy(data + left, src, bytes_per_row - left);
  395.        }
  396.     if ( actual_data != 0 )
  397.         *actual_data = data;
  398.     return 0;
  399. }
  400.  
  401. /* ------ The VESA device ------ */
  402.  
  403. private dev_proc_open_device(vesa_open);
  404. private gx_device_procs vesa_procs = svga_procs(vesa_open);
  405. int vesa_get_mode(P0());
  406. void vesa_set_mode(P1(int));
  407. private void vesa_set_page(P3(gx_device_svga *, int, int));
  408. gx_device_svga gs_vesa_device =
  409.     svga_device(vesa_procs, "vesa", vesa_get_mode, vesa_set_mode, vesa_set_page);
  410.  
  411. /* Define the structures for information returned by the BIOS. */
  412. #define bits_include(a, m) !(~(a) & (m))
  413. /* Information about the BIOS capabilities. */
  414. typedef struct {
  415.     byte vesa_signature[4];        /* "VESA" */
  416.     ushort vesa_version;
  417.     char *product_info;        /* product name string */
  418.     byte capabilities[4];        /* (undefined) */
  419.     ushort *mode_list;        /* supported video modes, -1 ends */
  420. } vga_bios_info;
  421. /* Information about an individual VESA mode. */
  422. typedef enum {
  423.     m_supported = 1,
  424.     m_graphics = 0x10
  425. } mode_attribute;
  426. typedef enum {
  427.     w_supported = 1,
  428.     w_readable = 2,
  429.     w_writable = 4
  430. } win_attribute;
  431. typedef struct {
  432.     ushort mode_attributes;
  433.     byte win_a_attributes;
  434.     byte win_b_attributes;
  435.     ushort win_granularity;
  436.     ushort win_size;
  437.     ushort win_a_segment;
  438.     ushort win_b_segment;
  439.     void (*win_func_ptr)(P2(int, int));
  440.     ushort bytes_per_line;
  441.         /* Optional information */
  442.     ushort x_resolution;
  443.     ushort y_resolution;
  444.     byte x_char_size;
  445.     byte y_char_size;
  446.     byte number_of_planes;
  447.     byte bits_per_pixel;
  448.     byte number_of_banks;
  449.     byte memory_model;
  450.     byte bank_size;
  451.         /* Padding to 256 bytes */
  452.     byte _padding[256-29];
  453. } vesa_info;
  454.  
  455. /* Read the device mode */
  456. int
  457. vesa_get_mode(void)
  458. {    registers regs;
  459.     regs.h.ah = 0x4f;
  460.     regs.h.al = 0x03;
  461.     int86(0x10, ®s, ®s);
  462.     return regs.rshort.bx;
  463. }
  464.  
  465. /* Set the device mode */
  466. void
  467. vesa_set_mode(int mode)
  468. {    registers regs;
  469.     regs.h.ah = 0x4f;
  470.     regs.h.al = 0x02;
  471.     regs.rshort.bx = mode;
  472.     int86(0x10, ®s, ®s);
  473. }
  474.  
  475. /* Read information about a device mode */
  476. private int
  477. vesa_get_info(int mode, vesa_info _ss *info)
  478. {    registers regs;
  479.     struct SREGS sregs;
  480.     regs.h.ah = 0x4f;
  481.     regs.h.al = 0x01;
  482.     regs.rshort.cx = mode;
  483.     segread(&sregs);
  484.     sregs.es = sregs.ss;
  485.     regs.rshort.di = PTR_OFF(info);
  486.     int86x(0x10, ®s, ®s, &sregs);
  487. #ifdef DEBUG
  488.     if ( regs.h.ah == 0 && regs.h.al == 0x4f )
  489.         dprintf8("vesa_get_info(%x): ma=%x wa=%x/%x wg=%x ws=%x wseg=%x/%x\n",
  490.              mode, info->mode_attributes,
  491.              info->win_a_attributes, info->win_b_attributes,
  492.              info->win_granularity, info->win_size,
  493.              info->win_a_segment, info->win_b_segment);
  494.     else
  495.         dprintf3("vesa_get_info(%x) failed: ah=%x al=%x\n",
  496.              mode, regs.h.ah, regs.h.al);
  497. #endif
  498.     return (regs.h.ah == 0 && regs.h.al == 0x4f ? 0 : -1);
  499. }
  500.  
  501. /* Initialize the graphics mode. */
  502. /* Shared routine to look up a VESA-compatible BIOS mode. */
  503. private int
  504. vesa_find_mode(gx_device *dev, const mode_info _ds *mode_table)
  505. {    /* Select the proper video mode */
  506.     vesa_info info;
  507.     const mode_info _ds *mip;
  508.     for ( mip = mode_table; mip->mode >= 0; mip++ )
  509.        {    if ( mip->width >= fb_dev->width &&
  510.              mip->height >= fb_dev->height &&
  511.              vesa_get_info(mip->mode, &info) >= 0 &&
  512.              bits_include(info.mode_attributes,
  513.             m_supported | m_graphics) &&
  514.              info.win_granularity == 64 &&
  515.              info.win_size == 64 &&
  516.              bits_include(info.win_a_attributes,
  517.             w_supported) &&
  518.              info.win_a_segment == regen
  519.            )
  520.            {    /* Make sure we can both read & write. */
  521.             /* Initialize for the default case. */
  522.             fb_dev->wnum_read = 0;
  523.             fb_dev->wnum_write = 0;
  524.             if ( bits_include(info.win_a_attributes,
  525.                 w_readable | w_writable)
  526.                )
  527.                 break;
  528.             else if ( info.win_b_segment == regen &&
  529.                 bits_include(info.win_b_attributes,
  530.                     w_supported) &&
  531.                 bits_include(info.win_a_attributes |
  532.                     info.win_b_attributes,
  533.                     w_readable | w_writable)
  534.                )
  535.                {    /* Two superimposed windows. */
  536.                 if ( !bits_include(info.win_a_attributes,
  537.                     w_writable)
  538.                    )
  539.                     fb_dev->wnum_write = 1;
  540.                 else
  541.                     fb_dev->wnum_read = 1;
  542.                }
  543.             break;
  544.            }
  545.        }
  546.     if ( mip->mode < 0 )
  547.         return_error(gs_error_rangecheck);    /* mode not available */
  548.     fb_dev->mode = mip;
  549.     gx_device_adjust_resolution(dev, mip->width, mip->height, 1);
  550.     fb_dev->info.vesa.bios_set_page = info.win_func_ptr;
  551.     /* Reset the raster per the VESA info. */
  552.     fb_dev->raster = info.bytes_per_line;
  553.     return 0;
  554. }
  555. private int
  556. vesa_open(gx_device *dev)
  557. {    static const mode_info mode_table[] = {
  558.        {     640,  400, 0x100    },
  559.        {     640,  480, 0x101    },
  560.        {     800,  600, 0x103    },
  561.        {    1024,  768, 0x105    },
  562.        {    1280, 1024, 0x107    },
  563.        {    -1, -1, -1    }
  564.     };
  565.     int code = vesa_find_mode(dev, mode_table);
  566.     if ( code < 0 ) return code;
  567.     return svga_open(dev);
  568. }
  569.  
  570. /* Set the current display page. */
  571. private void
  572. vesa_set_page(gx_device_svga *dev, int pn, int wnum)
  573. {
  574. #if USE_ASM
  575. extern void vesa_call_set_page(P3(void (*)(P2(int, int)), int, int));
  576.     if ( dev->info.vesa.bios_set_page != NULL )
  577.         vesa_call_set_page(dev->info.vesa.bios_set_page, pn, wnum);
  578.     else
  579. #endif
  580.        {    registers regs;
  581.         regs.rshort.dx = pn;
  582.         regs.h.ah = 0x4f;
  583.         regs.h.al = 5;
  584.         regs.rshort.bx = wnum;
  585.         int86(0x10, ®s, ®s);
  586.        }
  587. }
  588.  
  589. /* ------ The ATI Wonder device ------ */
  590.  
  591. private dev_proc_open_device(atiw_open);
  592. private gx_device_procs atiw_procs = svga_procs(atiw_open);
  593. private int atiw_get_mode(P0());
  594. private void atiw_set_mode(P1(int));
  595. private void atiw_set_page(P3(gx_device_svga *, int, int));
  596. gx_device_svga gs_atiw_device =
  597.     svga_device(atiw_procs, "atiw", atiw_get_mode, atiw_set_mode, atiw_set_page);
  598.  
  599. /* Read the device mode */
  600. private int
  601. atiw_get_mode(void)
  602. {    registers regs;
  603.     regs.h.ah = 0xf;
  604.     int86(0x10, ®s, ®s);
  605.     return regs.h.al;
  606. }
  607.  
  608. /* Set the device mode */
  609. private void
  610. atiw_set_mode(int mode)
  611. {    registers regs;
  612.     regs.h.ah = 0;
  613.     regs.h.al = mode;
  614.     int86(0x10, ®s, ®s);
  615. }
  616.  
  617. /* Initialize the graphics mode. */
  618. private int
  619. atiw_open(gx_device *dev)
  620. {    /* Select the proper video mode */
  621.        {    static const mode_info mode_table[] = {
  622.            {     640,  400, 0x61    },
  623.            {     640,  480, 0x62    },
  624.            {     800,  600, 0x63    },
  625.            {    1024,  768, 0x64    },
  626.            {    -1, -1, -1    }
  627.         };
  628.         int code = svga_find_mode(dev, mode_table);
  629.         if ( code < 0 ) return code;    /* mode not available */
  630.         fb_dev->info.atiw.select_reg = *(int *)MK_PTR(0xc000, 0x10);
  631.         return svga_open(dev);
  632.        }
  633. }
  634.  
  635. /* Set the current display page. */
  636. private void
  637. atiw_set_page(gx_device_svga *dev, int pn, int wnum)
  638. {    int select_reg = dev->info.atiw.select_reg;
  639.     byte reg;
  640.     disable();
  641.     outportb(select_reg, 0xb2);
  642.     reg = inportb(select_reg + 1);
  643.     outportb(select_reg, 0xb2);
  644.     outportb(select_reg + 1, (reg & 0xe1) + (pn << 1));
  645.     enable();
  646. }
  647.  
  648. /* ------ The Trident device ------ */
  649.  
  650. private dev_proc_open_device(tvga_open);
  651. private gx_device_procs tvga_procs = svga_procs(tvga_open);
  652. /* We can use the tseng_get/set_mode procedures. */
  653. private void tvga_set_page(P3(gx_device_svga *, int, int));
  654. gx_device_svga gs_tvga_device =
  655.     svga_device(tvga_procs, "tvga", atiw_get_mode, atiw_set_mode, tvga_set_page);
  656.  
  657. /* Initialize the graphics mode. */
  658. private int
  659. tvga_open(gx_device *dev)
  660. {       fb_dev->wnum_read = 1;
  661.     fb_dev->wnum_write = 0;
  662.     /* Select the proper video mode */
  663.        {    static const mode_info mode_table[] = {
  664.            {     640,  400, 0x5c        },
  665.            {     640,  480, 0x5d        },
  666.            {     800,  600, 0x5e        },
  667.            {     1024, 768, 0x62        },
  668.            {    -1, -1, -1      }
  669.         };
  670.         int code = svga_find_mode(dev, mode_table);
  671.         if ( code < 0 ) return code;      /* mode not available */
  672.         return svga_open(dev);
  673.        }
  674. }
  675.  
  676. /* Set the current display page. */
  677. private void
  678. tvga_set_page(gx_device_svga *dev, int pn, int wnum)
  679. {
  680.     /* new mode */
  681.     outportb(0x3c4, 0x0b);
  682.     inportb(0x3c4);
  683.  
  684.     outportb(0x3c4, 0x0e);
  685.     outportb(0x3c5, pn ^ 2);
  686. }
  687.  
  688. /* ------ The Tseng Labs ET3000/4000 device ------ */
  689.  
  690. private dev_proc_open_device(tseng_open);
  691. private gx_device_procs tseng_procs = svga_procs(tseng_open);
  692. /* We can use the tseng_get/set_mode procedures. */
  693. private void tseng_set_page(P3(gx_device_svga *, int, int));
  694. gx_device_svga gs_tseng_device =
  695.     svga_device(tseng_procs, "tseng", atiw_get_mode, atiw_set_mode, tseng_set_page);
  696.  
  697. /* Initialize the graphics mode. */
  698. private int
  699. tseng_open(gx_device *dev)
  700. {    fb_dev->wnum_read = 1;
  701.     fb_dev->wnum_write = 0;
  702.     /* Select the proper video mode */
  703.        {    static const mode_info mode_table[] = {
  704.            {     640,  350, 0x2d    },
  705.            {     640,  480, 0x2e    },
  706.            {     800,  600, 0x30    },
  707.            {     1024, 768, 0x38    },
  708.            {    -1, -1, -1    }
  709.         };
  710.         int code = svga_find_mode(dev, mode_table);
  711.         volatile_fb_ptr p0 = (volatile_fb_ptr)MK_PTR(regen, 0);
  712.         if ( code < 0 ) return code;    /* mode not available */
  713.         code = svga_open(dev);
  714.         if ( code < 0 ) return 0;
  715.         /* Figure out whether we have an ET3000 or an ET4000 */
  716.         /* by playing with the segment register. */
  717.         outportb(0x3cd, 0x44);
  718.         *p0 = 4;        /* byte 0, page 4 */
  719.         outportb(0x3cd, 0x40);
  720.         *p0 = 3;        /* byte 0, page 0 */
  721.         fb_dev->info.tseng.et_model = *p0;
  722.                     /* read page 0 if ET3000, */
  723.                     /* page 4 if ET4000 */
  724.         return 0;
  725.        }
  726. }
  727.  
  728. /* Set the current display page. */
  729. private void
  730. tseng_set_page(gx_device_svga *dev, int pn, int wnum)
  731. {    /* The ET3000 has read page = 5:3, write page = 2:0; */
  732.     /* the ET4000 has read page = 7:4, write page = 3:0. */
  733.     int shift = dev->info.tseng.et_model;
  734.     int mask = (1 << shift) - 1;
  735.     if ( wnum ) pn <<= shift, mask <<= shift;
  736.     outportb(0x3cd, (inportb(0x3cd) & ~mask) + pn);
  737. }
  738.